<template>
	<view>
		<u-form :model="long_order" ref="uForm">
			<u-form-item label="入住时间" class="content">
				
				<view class="right" @click="useDays" :style="!dayTxt?'opacity:0.6;':''">
							{{dayTxt?dayTxt:'请选择使用日'}}
						</view>
						<!-- 使用日start -->
						<u-popup :show="dayshow" mode="bottom" :round="20" @close="close" overlayOpacity="0.74" :closeable="true">
							<view class="checkDays">
								<view class="daytitle">
									选择可使用日
								</view>
								<view>
									<u-checkbox-group v-model="checkboxValue1" placement="column" @change="checkboxChange"
										iconPlacement="right" activeColor="#6C8FF8" size="38rpx">
										<u-checkbox :customStyle="{marginBottom: '40rpx'}" v-for="(item, index) in checkboxList1"
											:key="index" :label="item.name" :name="item.nums" labelSize="32rpx">
										</u-checkbox>
									</u-checkbox-group>
								</view>
								<view class="subBtn" @click="daysBtn()">
									确定
								</view>
							</view>
						</u-popup>
				
				
			
			</u-form-item>
			<u-form-item >
				入住校区<select >
					<option v-for="(campouss,index) in school" :value="campouss" :key="index">{{campouss}}
						
					</option>
				</select>
				
			</u-form-item>
			<u-form-item>
				上传文件<u-upload
						:fileList="fileList1"
						@afterRead="afterRead"
						@delete="deletePic"
						name="1"
						multiple
						:maxCount="10"
						:previewFullImage="true"
					></u-upload>
			</u-form-item>
			<u-form-item prop="photo"> 
				上传审批单<u-upload
						:fileList="fileList2"
						@afterRead="afterRead"
						@delete="deletePic"
						name="2"
						multiple
		
						
						:auto-upload="true"
						:maxCount="10"
						:previewFullImage="true"
					></u-upload>
			</u-form-item>
			<button @click="put" >提交</button>
		</u-form>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import * as dd from 'dingtalk-jsapi';
	export default {
		data() {
			return {dayshow: false,
				// 使用日
				checkboxValue1: [],
				dayTxt: '',
				// 数组
				checkboxList1: [{
						name: '周一',
						nums: 1,
						disabled: false
					},
					{
						name: '周二',
						nums: 2,
						disabled: false
					},
					{
						name: '周三',
						nums: 3,
						disabled: false
					},
					{
						name: '周四',
						nums: 4,
						disabled: false
					},
					{
						name: '周五',
						nums: 5,
						disabled: false
					},
					{
						name: '周六',
						nums: 6,
						disabled: false
					},
					{
						name: '周日',
						nums: 7,
						disabled: false
					}
				],

				long_order: {
					name: '',
					phone: '',
					checkInTime: '',
					campous: '',
					billTime:'',
					userId:''
					
				},
				weekArray: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
				modoule:'multiSelector',
				
				finalCampous:[],
				finalDate:[],
				fileList1: [],
				fileList2:[],
				showCampous:false,
				showDate:false,
				erweima:'',
				sindex:'',
				selectedWeeks:[],
				// index:0,
				value:[0],
				school:['小和山','安吉'],
				rules: {
					name:[{
						required: true,
						message:'请输入姓名',
						trigger: ['blur', 'change']
					}]
				}
			}
			
		},
		
		methods: {
			
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			close() { //关闭弹出层
							this.dayshow = false
						},
						// 使用天
						useDays() {
							console.log("选择可使用的日期");
							this.dayshow = true
						},
						checkboxChange(n) {
							console.log(n);
							this.checkboxValue1 = n
						},
						// 确定
						daysBtn() {
							if (this.checkboxValue1.length == 0) {
								uni.showToast({
									icon: 'none',
									title: '请选择天数'
								})
							} else {
								this.close()
								let dayTxt = []
								this.checkboxValue1.forEach((i) => {
									this.checkboxList1.forEach((item, index) => {
										if (item.nums == i) {
											dayTxt.push(item.name)
										}
									})
								})
								console.log(this.checkboxValue1)
								this.dayTxt = dayTxt.join()
							}
						},
			
			
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://localhost:8088/system/user/profile/avatar', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'avatarfile',
						formData: {
							user: 'test'
						},
						success: (res) => {
							console.log(res)
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},changeCampous(e){
				this.showDate=false
				
			},
			cancelChoose(e) {
							this.showCampous = false
							
						},
			sureChoose(e){
				this.showCampous=false;
				
				this.finalCampous=e.value[0];
				this.long_order.campous=e.value[0];
				
			},
			confirm(e) {
					this.showDate=false;
					console.log(e);
						this.finalDate=e.value[0];
						},
			put(){
				
				const token = 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImEyNzQxNTlmLTE4NGUtNDQ3Mi04ZmQ0LTQzNGMwMDFhODViZSJ9.Aj_IYFPEOtLpqlcEUxxi4XQkHCFvGTjPyrsTWJ6X-GyY25IsGVIWXCNU0g4zGMV8pm3hCgACtc1NsIqw3tnPDg';
				this.$refs.uForm.validate().then(res => {
								uni.$u.toast('校验通过');
								  setTimeout(() => {
								    // 跳转页面
								    window.location.href = '/pages/index/index';
								  }, 2000); // 延迟2秒后跳转
							}).catch(errors => {
								uni.$u.toast('校验失败')
							});
				
				uni.request({
					url:'http://localhost:8088/system/bill',
					method:'POST',
					data:{
						name:this.long_order.name,
						userId:this.userId
						
					},
						
					header:{ 'Content-Type': 'application/json',
						'Authorization':'Bearer'+token},
						
					
					
					
					success:(res)=>{
						
					}
				});
				
				
				
			},
			 onWeekChange(e) {
				 console.log(e)
			      // this.selectedWeeks = value;
			    },
			cancelDate(){
				this.showDate = false
			},
			
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
			dd.getAuthCode({
							corpId: 'ding0a40cdce33c8deb624f2f5cc6abecb85',
							success: (res) => {
								const {code}=res
									uni.request({
										url:`http://localhost:8088/system/dingding/v2/user/${code}`,
										success:(res)=> {
											const{userid}=res.data.result
											
											this.userId=userid
											
										}
										
									})
											  },
						    fail: () => {},
							complete: () => {},
							});
		},
			
			
			
		}
		
		
	
</script>

<style>
.checkDays {
		padding: 0 30rpx;
		font-size: 34rpx;

		.daytitle {
			text-align: center;
			font-size: 34rpx;
			font-weight: bold;
			padding: 40rpx 0;
		}

		.subBtn {
			
			margin: 40rpx 30rpx;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			background: #6C8FF8;
			box-shadow: 0px 0px 24rpx 0px rgba(202, 202, 202, 0.41);
			border-radius: 50rpx;
			font-size: 32rpx;
			font-weight: bold;
			color: #FFFFFF;
		}
	}

</style>
